import {Component, OnInit} from '@angular/core';
import * as CryptoJS from 'crypto-js';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  param = '';
  key = '';
  result1 = '';
  result2 = '';

  ngOnInit(): void {
    this.param = 'test123';
    this.key = '11233333';
    this.result1 = this.encrypt(this.param, this.key);
    this.result2 = this.decrypt(this.result1, this.key);
  }

  encrypt(content, key): string {
    const key1 = CryptoJS.enc.Utf8.parse(key);
    const content1 = CryptoJS.enc.Utf8.parse(content);
    const result = CryptoJS.AES.encrypt(content1, key1, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    return result.toString();
  }

  decrypt(content, key): string {
    const key1 = CryptoJS.enc.Utf8.parse(key);
    const decrypt = CryptoJS.AES.decrypt(content, key1, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
  }
}
